/* eslint-disable vue/valid-v-for */
<template>
  <div class="app-container">
    <!--搜索条件-->
    <div class="filter-container">
      <el-row :gutter="13" type="flex">
        <!-- @blur="searchSku" -->
        <el-col :span="4">
          <el-input v-model="search" placeholder="输入查询" />
        </el-col>

        <el-col :span="1">
          <el-button type="primary" @click="searchLianbaoBrand">查询</el-button>
        </el-col>

        <el-col :span="1">
          <el-button type="primary" @click="addFun()">新增</el-button>
        </el-col>
      </el-row>
    </div>

    <div class="table-ymm">
      <el-table
        v-loading="listLoading"
        :data="lianbaoBrandList"
        border
        highlight-current-row
        style="width: 95%;"
      >
        <el-table-column align="center" label="操作" width="300">
          <template slot-scope="scope">
            <el-button type="primary" size="small" icon="el-icon-edit" @click="edictFun(scope)">编辑</el-button>
            <el-button type="primary" size="small" icon="el-icon-delete" @click="delFun(scope)">删除</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="brandName" label="品牌名称" min-width="5%" />
      </el-table>
    </div>

    <!--分页-->
    <div class="pagination-container">
      <el-pagination
        v-show="total>0"
        :current-page="listQuery.page"
        :page-sizes="[10,20,30, 50]"
        :page-size="listQuery.pageSize"
        :total="total"
        background
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <component
      :is="modal"
      :modal-data="modalData"
      :modal-static-data="{'title':modalData.title,closeChildAndRefresh}"
      @close="closeChild"
    />
  </div>
</template>

<script>
// post
import { get, post } from '@/api/utils'
import Add from './dialog/Add.vue'

export default {
  data() {
    return {
      search: '',
      dialogFormVisible: false,
      total: 1,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 10,
        keyword: null,
        starTime: null,
        endTime: null,
        shipmentNo: null,
        time: null,
        pageNumber: null
      },
      lianbaoBrandList: [],
      modal: null,
      modalData: {}
    }
  },
  created() {
    this.getLianbaoBrandList()
  },

  methods: {
    searchLianbaoBrand(event) {
      this.listQuery.page = 1
      this.getLianbaoBrandList()
    },

    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getLianbaoBrandList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getLianbaoBrandList()
    },

    addFun() {
      this.modalData = { type: 'creat', title: '新增品牌' }
      this.modal = Add
    },
    edictFun(data) {
      var lianbaoBrand = data.row
      console.log(lianbaoBrand)
      this.modalData = { type: 'edict', title: '修改品牌', lianbaoBrand }
      this.modal = Add
    },
    delFun(data) {
      var lianbaoBrand = data.row
      this.$confirm(`您确定删除${lianbaoBrand.brandName}品牌?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          var data = { brandId: lianbaoBrand.brandId }
          post('/wxh-site-rest/rest/', '/webpoint/brand/delete', data)
            .then(response => {
              if (response.data.status === 'success') {
                console.log(response)
                this.$message({
                  message: '删除品牌成功'
                })
                this.listQuery.page = 1
                this.getLianbaoBrandList()
              } else {
                this.$message({
                  message: response.data.messge,
                  type: 'warning'
                })
              }
            })
            .catch(error => {
              console.log(error)
              this.$message({
                message: '数据加载失败',
                type: 'warning'
              })
            })
        })
        .catch(e => {
          console.log(e)
          this.$message({
            type: 'info',
            message: '已取消当前操作'
          })
        })
    },
    closeChild() {
      this.modal = null
    },
    closeChildAndRefresh() {
      this.modal = null
      this.listQuery.page = 1
      this.getLianbaoBrandList()
    },

    getLianbaoBrandList() {
      this.listLoading = true

      this.listQuery.pageNumber =
        (this.listQuery.page - 1) * this.listQuery.pageSize

      var data = {
        offset: this.listQuery.pageNumber,
        count: this.listQuery.pageSize
      }

      if (this.search !== null) {
        data.search = this.search
      }

      get('/wxh-site-rest/rest/', '/webpoint/brand/list', data)
        .then(response => {
          if (response.data.status === 'success') {
            this.lianbaoBrandList = response.data.data
            console.log(response.data.count)
            this.total = response.data.count
          } else if (response.data.status === 'token_check_faild') {
            this.$message({
              message: response.data.messge,
              type: 'warning'
            })
          }
          this.listLoading = false
        })
        .catch(error => {
          console.log(error)
          this.$message({
            message: '供应商数据加载失败',
            type: 'warning'
          })
          this.listLoading = false
        })
    }
  }
}
</script>
